var _shopId;
var _openId;
var _goodsId;
$(function() {
	_shopId = qmsk.getQueryValue("shopId");
	_openId = qmsk.getQueryValue("openId");
	_goodsId = qmsk.getQueryValue("goodsId");

	if (qmsk.isEmpty(_goodsId)) {
		alert('缺少 goodsId,请联系客服!');
		return;
	}
	loadShopInfo(_goodsId);
	InitCount();
});

function loadShopInfo(goodsId) {
	qmsk.get('../goods/find/goodsId/' + goodsId, printDataToPage);
}

/**
 * 将商品信息打印到页面上
 */
function printDataToPage(response) {

	var goodsId = response.getValue("goodsID");

	_goodsId = goodsId;
	var goodsName = response.getValue("goodsName");
	var typeSmlName = response.getValue("typeSmlName");
	var mode = response.getValue("mode");
	var size = response.getValue("size");
	var salePrice = response.getValue("salePrice");
	var costPrice = response.getValue("costPrice");
	var barCode = response.getValue("barCode");
	var imageUrl = response.getValue("imageUrl");
	var indexUrl = response.getValue("indexUrl");
	var count = response.getValue("count");
	var saleIntegral = response.getValue("saleIntegral");

	$("#imagePanel").empty();
	$("#imageNums").attr("data-count", "1 张图片");
	$("#imagePanel").append("<img onload='preViewImg(this, event);' data-gid='g0' data-src='" + imageUrl + "' src='" + imageUrl + "'>");

	$("#imageUrl").attr("src", imageUrl);
	$("#tpSmlName").text("•	所属系列：" + typeSmlName);
	$("#name").text(goodsName);
	$("#mode").text("•	规格：" + mode);
	$("#size").text("•	尺寸：" + size + "（mm）");
	//$("#price").html("•	商城价：" + salePrice + "元" + "<del style='margin-left:10px;color:#666;'>" + costPrice + "</del>");
	$("#saleIntegral").html("•	兑换积分：" + saleIntegral);
	$("#barCode").text("•	产品条码编号：" + barCode);
	$("#num").val(1);
	loadHtmlFile(indexUrl);
}

function loadHtmlFile(url) {
	qmsk.post('../htmlUtils/find/getHtml', {
		'htmlUrl' : url
	}, function(response) {
		var html = response.getValue("htmlStr");
		$("#indexUrl").html(html);
	});
}

function caseGoos() {
	var num = $("#num").val();
	num = parseInt(num);
	if (num <= 0)
		return;
	qmsk.post('../cart/add/' + _shopId + '/' + _goodsId + '/' + _openId, {
		num : num
	}, function(response) {
		InitCount();
		alert("添加到购物车成功！");
	});

	InitCount();
}

/**
 * 物品数量增加
 */
function upNum() {
	var num = $("#num").val();
	if (num >= 0) {
		$("#num").val(parseInt(num) + 1);
	}
}

/**
 * 物品数量减少
 */
function downNum() {
	var num = $("#num").val();
	if (num > 1) {
		$("#num").val(parseInt(num) - 1);
	}
}

/**
 * 改变商品数量
 */
function changeNum(id) {
	var value = $("#" + id).val();
	var num = parseInt(value);
	if (!(num >= 0)) {
		num = 0;
	}
	$("#" + id).val(num);
}

//预览图
window.preViewImg = (function() {
	var imgsSrc = {};
	function reviewImage(dsrc, gid) {
		if ( typeof window.WeixinJSBridge != 'undefined') {
			WeixinJSBridge.invoke('imagePreview', {
				'current' : dsrc,
				'urls' : imgsSrc[gid]
			});
		} else {
			alert("请在微信中查看", null, function() {
			});
		}
	}

	function init(thi, evt) {
		var dsrc = thi.getAttribute("data-src");
		var gid = thi.getAttribute("data-gid");

		if (dsrc && gid) {
			imgsSrc[gid] = imgsSrc[gid] || [];
			imgsSrc[gid].push(dsrc);
			thi.addEventListener("click", function() {
				reviewImage(dsrc, gid);
			}, false);
		}
	}

	return init;
})();

/**
 * 加载购物车 数量
 */
function InitCount() {
	qmsk.get('../cart/cartNums/' + _openId, function(response) {
		var count = response.getValue('count');
		if (count > 0) {
			$("#sup").text("(" + count + ")");
		} else {
			$("#sup").text('');
		}
	});
}

/**
 * 到购物车页面
 */
function toBuyCar() {
	qmsk.redirectUrl('buy_car.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 刷新
 */
function myrefresh() {
	window.location.reload();
}

/**
 * 商品列表页面
 */
function toOrderForm() {
	qmsk.redirectUrl('order_form.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 商品列表页面
 */
function toProducts() {
	qmsk.redirectUrl('products.html', {
		shopId : _shopId,
		openId : _openId
	});
}
